package com.jackchen.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.jackchen.common.transform.TransformException;
import com.jackchen.common.transform.Transformer;
import com.jackchen.dao.ContentDAO;
import com.jackchen.dao.ResourceDAO;
import com.jackchen.dao.ResourcetypeDAO;
import com.jackchen.dao.UserDAO;
import com.jackchen.po.Content;
import com.jackchen.po.Resource;
import com.jackchen.po.Resourcetype;
import com.jackchen.po.User;
import com.jackchen.service.ResourceService;
import com.jackchen.vo.DiaryVO;

public class ResourceServiceImpl extends IBaseServiceImpl implements ResourceService {
	private static final Logger log = Logger.getLogger(ResourceServiceImpl.class);
	
	private ResourceDAO resourceDAO;
	private UserDAO userDAO;
	private ContentDAO contentDAO;
	private ResourcetypeDAO resourcetypeDAO;

	public ResourceDAO getResourceDAO() {
		return resourceDAO;
	}

	public void setResourceDAO(ResourceDAO resourceDAO) {
		this.resourceDAO = resourceDAO;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public ContentDAO getContentDAO() {
		return contentDAO;
	}

	public void setContentDAO(ContentDAO contentDAO) {
		this.contentDAO = contentDAO;
	}

	public ResourcetypeDAO getResourcetypeDAO() {
		return resourcetypeDAO;
	}

	public void setResourcetypeDAO(ResourcetypeDAO resourcetypeDAO) {
		this.resourcetypeDAO = resourcetypeDAO;
	}

	@Override
	public Map queryDiaryOnPage(String userName, String startTime,
			String endTime, int pageSize, int startRow) {
		String baseSql = " from Resource res where 1=1";
		StringBuffer hsql = new StringBuffer(baseSql);
		// 添加用户名查询条件
		if (userName != null && !"".equals(userName.trim())) {
			hsql.append(" AND (res.user.userName like '%" + userName.trim() + "%'");
			hsql.append(")");
		}
		// 添加日期条件查询
		if(startTime != null && !"".equals(startTime.trim()) && endTime != null && !"".equals(endTime.trim())) {
			hsql.append(" AND (res.uploadtime between '"+startTime+"' and '"+endTime+"' ) ");
		}else if(startTime != null && !"".equals(startTime.trim()) &&(( endTime == null) || "".equals(endTime.trim()))) {
			hsql.append(" AND (res.uploadtime = '"+ startTime +"' ) ");
		}else if((startTime == null || "".equals(startTime.trim()))&& endTime != null && !"".equals(endTime.trim())) {
			hsql.append(" AND (res.uploadtime = '"+ endTime +"' ) ");
		}
		
		Map logMap = resourceDAO.findObjBySqlPage(hsql.toString(), pageSize, startRow);
		List logList= (List<Resource>) logMap.get("querylist");
		List<DiaryVO> dataList = null;
		try {
			dataList = Transformer.pojoToVos(logList, DiaryVO.class);
		} catch (TransformException e) {
			// 数据转换失败
			log.error(e.getMessage());
			dataList = Collections.emptyList();
		}
		logMap.put("querylist", dataList);
		return logMap;

	}
	
//	@Transactional(rollbackFor=Exception.class)
	@Override
	public boolean createNewDiary(String name, String author,
			String content) {
		User u = userDAO.findById(User.class, 1);
		Content c = new Content(content);
		contentDAO.save(c);
		Resourcetype resType = resourcetypeDAO.findById(Resourcetype.class, 5);

		Resource res = new Resource(c, resType, u,
				name, 11, author, new Date(),
				new Date());
		resourceDAO.save(res);
		return true;
	}

	@Override
	public DiaryVO queryDiaryById(String diaryId) {
		// TODO Auto-generated method stub
		if(diaryId==null){
			return null;
		}
		Integer id = Integer.parseInt(diaryId);
		if(id!=null){
			Resource res = resourceDAO.findById(Resource.class, id);
			try {
				return Transformer.pojoToVo(res, DiaryVO.class);
			} catch (TransformException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public boolean updateDiary(String diaryid, String diaryname,
			String realname, String content, int languageFlag) {
		Resource r = resourceDAO.findById(Resource.class, Integer.parseInt(diaryid));
		r.setAuthor(realname);
		r.setName(diaryname);
		r.getContent().setContent(content);
		resourceDAO.update(r);
		return true;
	}

}
